//
//  CXDiscoverSearchView.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/18.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit

class CXDiscoverSearchView: UIView {

    @IBOutlet weak var textFieldConstrain: NSLayoutConstraint!
    
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var textFieldView: CXTextField!

    @IBAction func beginEdit(sender: UITextField) {
        
        textFieldConstrain.constant = cancelBtn.frame.width
        UIView.animateWithDuration(1.0) {
            self.layoutIfNeeded()
        }
    }
    //MARK:在从nib中创建后添加textField的左视图
    //不要在initWithCode或者initWithFrame中添加，因为那时候，连线的控件还未生成
    override func awakeFromNib() {
        textFieldView.leftView = leftView
        textFieldView.leftViewMode = .Always
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        leftView.frame.size = CGSize(width: frame.height, height: frame.height) //让leftView变成一个正方形
    }
    
    
    //MARK: 类方法 返回searchView实例
    class func getSearchView() -> CXDiscoverSearchView{
        return NSBundle.mainBundle().loadNibNamed("CXDiscoverSearchView", owner: nil, options: nil).last! as! CXDiscoverSearchView
    }

    @IBAction func cancelClick(sender: UIButton) {
        
        textFieldConstrain.constant = 0
        UIView.animateWithDuration(1.0) {
            self.layoutIfNeeded()
        }
        //取消文本框的第一响应者身份
        textFieldView.resignFirstResponder()
    }
    
    //懒加载textField的leftView
    private lazy var leftView:UIImageView = {
        let lv = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        //设置内容的填充模式
        lv.contentMode = .Center
//        lv.bounds.origin.x = -5
        return lv
    }()
}
